package ru.usu.gv.utils.exports;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.media.j3d.Alpha;

/**
 @author Sergey Pupyrev 
 29.05.2007
 */

public class MovieTool
{
    public static void generateVideo(OffScreenCanvas3D offScreenCanvas3D, long animationTime, Alpha alpha, File outputFile, int fps, int width, int height)
    {
        /* Initializing the animation */
        long startTime = alpha.getStartTime();
        alpha.pause(startTime);

        /* Milliseconds it takes to change the frame. */
        long msFrame = (long)(((float)1 / fps) * 1000);

        /* Frames you need to create the movie */
        long framesNeeded = animationTime / msFrame;

        System.out.println("startTime=" + startTime);
        System.out.println("msFrame=" + msFrame);
        System.out.println("framesNeeded=" + framesNeeded);

        /* Create an offscreen canvas from where you'll get the image */
        /*
         * CREATE FRAMES
         */

        String name = "img_";
        String tempDir = System.getProperty("user.dir");

        File fTempDir = new File(tempDir + System.getProperty("file.separator") + "tempVideo");
        fTempDir.mkdir();

        //if (1 == 1) return;
        Vector<File> vFiles = new Vector<File>();

        System.gc();

        for (long frameCount = 0; frameCount <= framesNeeded; frameCount++)
        {
            String tempName = name + frameCount;
            File file = new File(fTempDir, tempName + ".jpg");
            if (file.exists())
            {
                vFiles.add(file);
                continue;
            }

            /* Stop the animation in the correct position */
            long time = startTime + frameCount * msFrame;
            alpha.pause(time);

            /* Get the renderized image at that moment */
            BufferedImage bi = offScreenCanvas3D.doRender(width, height);

            /* Create a JPEG file from the renderized image */

            //FileOutputStream out = new FileOutputStream(path);
            try
            {
                ImageIO.write(bi, "JPG", file);
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
                return;
            }

            vFiles.add(file);
        }

        //temp
        if (1 == 1)
            return;
        /*
         * GENERATE VIDEO
         */

        int frameRate = (int)((framesNeeded + 1) / (animationTime / 1000));

        File[] files = vFiles.toArray(new File[0]);

        try
        {
            if (outputFile.exists())
            {
                outputFile.delete();
                outputFile.createNewFile();
            }

            //MovieMaker movieMaker = new MovieMaker(width, height, frameRate, outputFile, files);

            //movieMaker.makeMovie();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

}
